package com.zzc.legou.service.item.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzc.legou.core.controller.BaseController;
import com.zzc.legou.item.entity.Category;

import com.zzc.legou.log.aop.annotation.LogAnno;
import com.zzc.legou.service.item.service.ICategoryService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/category")

public class CategoryController extends BaseController<ICategoryService, Category> {

    @Override
    public List<Category> list(Category entity) {

        QueryWrapper<Category> queryWrapper = Wrappers.query();
        //根据名称进行查询
        if (!StringUtils.isEmpty(entity.getTitle())) {
            queryWrapper.like("title_", entity.getTitle());
        }
        if (null != entity.getParentId()) {
            queryWrapper.eq("parent_id_", entity.getParentId());
        }
        if (null != entity.getIsRoot() && entity.getIsRoot().equals(1)) {
            queryWrapper.isNull("parent_id_");
        }

        return this.service.list(queryWrapper);
    }

    /**
     * 根据ids查询names
     */
    @GetMapping("/names")
    public ResponseEntity<List<String>> queryNameByIds(@RequestParam("ids") List<Long> ids) {
        List<Category> list = service.queryNameByIds(ids);
        System.out.println("list = " + list);
        List<String> names = list.stream().map(item -> item.getTitle()).collect(Collectors.toList());

        if (ObjectUtils.isEmpty(names)) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(names);
    }
}
